package com.cyt.work.kwmanagement.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.base.util.StringUtils;
import com.cyt.work.kwmanagement.dao.KwStudentSignDao;
import com.cyt.work.kwmanagement.model.po.KwStudentSign;
import com.cyt.work.kwmanagement.model.vo.KwKmMessageVo;
import com.cyt.work.kwmanagement.model.vo.KwStudentSignVo;
import com.cyt.work.kwmanagement.service.KwStudentSignService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.wx.model.po.WxMember;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 学生签到表-学生签到表Service层实现类
 *
 * @author Han Jun
 * @date 2019-12-11 15:33:49
 */
@Service("kwStudentSignService")
public class KwStudentSignServiceImpl extends BaseServiceImpl<KwStudentSign> implements KwStudentSignService {

    @Resource
    private KwStudentSignDao kwStudentSignDao;

    @Autowired
    public void setBaseDao(KwStudentSignDao kwStudentSignDao) {
        this.baseDao = kwStudentSignDao;
    }

    /**
     * 分页查询
     *
     * @param kwStudentSignVo 接受参数对象
     * @param pageNum 分页数
     * @param pageSize 每页显示条数
     * @return ReturnResult
     * @author Han Jun
     * @date 2019-12-11 15:33:49
     */
    @Override
    public ReturnResult findPage(KwStudentSignVo kwStudentSignVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        Example example = new Example(KwStudentSign.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        List<KwStudentSign> list = kwStudentSignDao.selectByExample(example);
        PageInfo<KwStudentSign> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 分页查询
     *
     * @param kwStudentSignVo 接受参数对象
     * @param pageNum 分页数
     * @param pageSize 每页显示条数
     * @return ReturnResult
     * @author Han Jun
     * @date 2019-12-11 15:33:49
     */
    @Override
    public ReturnResult findVoPage(KwStudentSignVo kwStudentSignVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<KwStudentSignVo> list = kwStudentSignDao.selectVoList(kwStudentSignVo);
        PageInfo<KwStudentSignVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * list查询
     *
     * @param kwStudentSignVo 接受参数对象
     * @return List<KwStudentSign>
     * @author Han Jun
     * @date 2019-12-11 15:33:49
     */
    @Override
    public List<KwStudentSign> findList(KwStudentSignVo kwStudentSignVo) {
        Example example = new Example(KwStudentSign.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return kwStudentSignDao.selectByExample(example);
    }

    /**
     * list查询
     *
     * @param kwStudentSignVo 接受参数对象
     * @return List<KwStudentSign>
     * @author Han Jun
     * @date 2019-12-11 15:33:49
     */
    @Override
    public List<KwStudentSignVo> findVoList(KwStudentSignVo kwStudentSignVo) {
        return kwStudentSignDao.selectVoList(kwStudentSignVo);
    }

    /**
     * 单个对象查询
     *
     * @param kwStudentSignVo 接受参数对象
     * @return KwStudentSign
     * @author Han Jun
     * @date 2019-12-11 15:33:49
     */
    @Override
    public KwStudentSign findOne(KwStudentSignVo kwStudentSignVo) {
        KwStudentSign kwStudentSign = new KwStudentSign();
        if (StringUtils.isBlank(kwStudentSignVo.getId())) {
            kwStudentSign.setId(kwStudentSignVo.getId());
        }
        return kwStudentSignDao.selectOne(kwStudentSign);
    }

    /**
     * 单个对象查询
     *
     * @param kwStudentSignVo 接受参数对象
     * @return KwStudentSign
     * @author Han Jun
     * @date 2019-12-11 15:33:49
     */
    @Override
    public KwStudentSignVo findVoOne(KwStudentSignVo kwStudentSignVo) {
        return kwStudentSignDao.selectVoOne(kwStudentSignVo);
    }

    /**
     * 添加或更新
     *
     * @param kwStudentSignVo 参数接受对象
     * @param wxMember      当前用户
     * @return ReturnResult
     * @author Han Jun
     * @date 2019-12-11 15:33:49
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(KwStudentSignVo kwStudentSignVo, WxMember wxMember) {
        if (StringUtils.isBlank(kwStudentSignVo.getId())) {
            KwStudentSign kwStudentSign = new KwStudentSign(wxMember);
            kwStudentSign.setApplyId(kwStudentSignVo.getApplyId());
            kwStudentSign.setKsId(kwStudentSignVo.getKsId());
            kwStudentSign.setKmId(kwStudentSignVo.getKmId());
            kwStudentSign.setEducationUserid(kwStudentSignVo.getEducationUserid());
            kwStudentSign.setKcId(kwStudentSignVo.getKcId());
            kwStudentSign.setSignState(kwStudentSignVo.getSignState());
            kwStudentSignDao.insert(kwStudentSign);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            KwStudentSign kwStudentSign = kwStudentSignDao.selectByPrimaryKey(kwStudentSignVo.getId());
            kwStudentSign.setApplyId(kwStudentSignVo.getApplyId());
            kwStudentSign.setKsId(kwStudentSignVo.getKsId());
            kwStudentSign.setKmId(kwStudentSignVo.getKmId());
            kwStudentSign.setEducationUserid(kwStudentSignVo.getEducationUserid());
            kwStudentSign.setKcId(kwStudentSignVo.getKcId());
            kwStudentSign.setSignState(kwStudentSignVo.getSignState());
            kwStudentSign.setUpdateTime(new Date());

            kwStudentSign.setUpdateUser(wxMember.getId());
            kwStudentSignDao.updateByPrimaryKey(kwStudentSign);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }




    /**
     * 根据条件逻辑删除
     *
     * @param kwStudentSignVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author Han Jun
     * @date 2019-12-11 15:33:49
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(KwStudentSignVo kwStudentSignVo, SysUser sysUser) {
        //修改字段
        KwStudentSign kwStudentSign = new KwStudentSign();
        kwStudentSign.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        kwStudentSign.setUpdateUser(sysUser.getId());
        kwStudentSign.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(KwStudentSign.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwStudentSignVo.getId())) {
            criteria.andIn("id", Arrays.asList(kwStudentSignVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        kwStudentSignDao.updateByExampleSelective(kwStudentSign, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param kwStudentSignVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author Han Jun
     * @date 2019-12-11 15:33:49
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(KwStudentSignVo kwStudentSignVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(KwStudentSign.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwStudentSignVo.getId())) {
            criteria.andIn("id", Arrays.asList(kwStudentSignVo.getId().split(",")));
        }
        kwStudentSignDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdateAll(String applyId ,String kwKmSignMessageJson, WxMember wxMember) {
        List<KwStudentSignVo> kmMessageDTOList = JSON.parseArray(kwKmSignMessageJson, KwStudentSignVo.class);
        for (int i = 0; i < kmMessageDTOList.size(); i++) {
            kmMessageDTOList.get(i).setApplyId(applyId);
            addOrUpdate(kmMessageDTOList.get(i),  wxMember);
        }
        return new ReturnResult();
    }

}